<?php

namespace App\Admin\Actions\Grid\Row\SwtTyGood;

use App\Models\SwtDheOrder;
use App\Models\SwtOrder;
use App\Models\SwtTyGood;
use App\Services\DaHuangService;
use App\Services\TuoYuanService;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RefreshStatus extends RowAction
{
    /**
     * @return string
     */
    protected $title = '更新';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $id = $request->get('id');
        $acceptance_id = $request->get('acceptance_id');

        $res = TuoYuanService::getApplyGoodsStatus($acceptance_id);

        SwtTyGood::where('id', $id) -> update(['is_register' => $res['status'],'desc' => $res['message']]);

        return $this->response()
            ->success('更新成功')
            ->refresh();
    }

    /**
     * @return string|array|void
     */
    public function confirm()
    {
        return ['确认操作吗?', '手动更新审核状态'];
    }

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [
            'id'              => $this->row->id,
            'acceptance_id'   => $this->row->acceptance_id
        ];
    }

    protected function html()
    {
        return <<<HTML
            <a {$this->formatHtmlAttributes()}><i class="feather icon-refresh-ccw text-info"></i> <span class="text-info">{$this->title}</span> &emsp;</a>
        HTML;
    }
}
